<?php
/**
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package    Fuel
 * @version    1.7
 * @author     Fuel Development Team
 * @license    MIT License
 * @copyright  2010 - 2013 Fuel Development Team
 * @link       http://fuelphp.com
 */

/**
 * Set error reporting and display errors settings.  You will want to change these when in production.
 */
error_reporting(-1);
ini_set('display_errors', 1);

/**
 * Website document root
 */
define('DOCROOT', __DIR__ . DIRECTORY_SEPARATOR);

/**
 * Path to the application directory.
 */
define('APPPATH', realpath(__DIR__ . '/../fuel/app/') . DIRECTORY_SEPARATOR);

/**
 * Path to the default packages directory.
 */
define('PKGPATH', realpath(__DIR__ . '/../fuel/packages/') . DIRECTORY_SEPARATOR);

/**
 * The path to the framework core.
 */
define('COREPATH', realpath(__DIR__ . '/../fuel/core/') . DIRECTORY_SEPARATOR);

// Get the start time and memory for use later
defined('FUEL_START_TIME') or define('FUEL_START_TIME', microtime(true));
defined('FUEL_START_MEM') or define('FUEL_START_MEM', memory_get_usage());

// Boot the app
require APPPATH . 'bootstrap.php';

// Generate the request, execute it and send the output.
try {
    $response = Request::forge()->execute()->response();
}catch (OpenIdRequireException $e){
    echo "haha  ";exit;
}
/* catch (ApiException $e){
    $response = new \Fuel\Core\Response();
    $response->set_header('Content-Type', "application/json");

    $response->body(
        Format::forge(array(
            'code'=>$e->code,
            'errorMessage'=>$e->message
        ))->to_json()
    );
}*/ catch (UserLoginRequireException $e) {
    if (isset($e->uri)&& $e->uri!='') {
        Session::instance()->set('AFTER_LOGIN_URI', $e->uri);
    }
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pramga: no-cache");
//    Response::redirect('/mobile/login');
    Response::redirect( Config::get('user.login.url') ."?nextUrl=" . urlencode($e->uri) ) ;

} catch (PermissionRequireException $e) {
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pramga: no-cache");
    Response::redirect( Uri::create('admin/login'));
} catch (HttpNotFoundException $e) {
    \Request::reset_request(true);
    $route = array_key_exists('_404_', Router::$routes) ? Router::$routes['_404_']->translation : Config::get('routes._404_');
    if ($route instanceof Closure) {
        $response = $route();
        if (!$response instanceof Response) {
            $response = Response::forge($response);
        }
    } elseif ($route) {
        $response = Request::forge($route, false)->execute()->response();
    } else {
        throw $e;
    }
}

// Render the output
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pramga: no-cache");


$response->body((string)$response);

// This will add the execution time and memory usage to the output.
// Comment this out if you don't use it.
if (strpos($response->body(), '{exec_time}') !== false or strpos($response->body(), '{mem_usage}') !== false) {
    $bm = Profiler::app_total();
    $response->body(
        str_replace(
            array('{exec_time}', '{mem_usage}'),
            array(round($bm[0], 4), round($bm[1] / pow(1024, 2), 3)),
            $response->body()
        )
    );
}

$response->send(true);
